{% extends "cms/cms_base.html" %}

{%block head%}
<style>
    .yan{
        width:300px;
    }
    #basic-addon1{
        width:100px;
    }
</style>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
$(document).ready(function(eq) {
    var csrftoken = $('meta[name=csrf_token]').attr('content')
    var addon1 = $('#basic-addon1')
    var check = $('input[name=yan_zheng]')
    var addon2 = $('#basic-addon2')
    addon1.click(function (event) {
        event.preventDefault()
        var email = $('input[name=newmail]').val()
        var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
        if (reg.test(email)) {
            swal({
                icon: "success",
                text: "已经发送" + email
            })
        } else {
            swal({
                icon: "error",
                text: "邮箱格式不正确"
            })
        }

        $.ajax({
            url: "http://127.0.0.1:5000/cms/getNum/",
            type: "POST",
            headers: {"X-CSRFToken": csrftoken},
            data: {
                "email": email,
                "yan_zheng": check.val()
            },
            success: function (data) {
                console.log(data)
                icon = "success"
                title = "Success"
                if (data.code == 400) {
                    icon = "error"
                    title = "Error"
                }
                swal({
                    title: "Success!",
                    text: data.message,
                    icon: icon,
                });
            },
            error: function (data) {
                console.log(data)
                swal({
                    title: "Error!",
                    text: data.message,
                    icon: 'error'
                })
            }
        })

    })


    $("#submit_email").click(function (event) {
        event.preventDefault()
        var email = $('input[name=newmail]').val()
        yan_zheng = check.val()
        // console.log(csrftoken)
        var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
        if (reg.test(email)) {
            swal({
                icon: "success",
                text: "已经发送" + email
            })
        } else {
            swal({
                icon: "error",
                text: "邮箱格式不正确"
            })
        }

        $.ajax({
            url: "http://127.0.0.1:5000/cms/getNum/",
            type: "POST",
            headers: {"X-CSRFToken": csrftoken},
            data: {
                "yan_zheng": yan_zheng,
                "email": email
            },
            success: function (data) {
                console.log(data)
                icon = "success"
                title = "Success"
                if (data.code == 400) {
                    icon = "error"
                    title = "Error"
                }
                swal({
                    title: "Success!",
                    text: data.message,
                    icon: icon,
                });
            },
            error: function (data) {
                console.log(data)
                swal({
                    title: "Error!",
                    text: data.message,
                    icon: 'error'
                })
            }
        })
    })
})
</script>
{%endblock%}



{% block pagetitle%}
修改邮箱
{% endblock %}

{% block content %}
<form method="post" action="{{url_for('cms.getNum')}}">
    <div class="form-group input-group yan">
  <input type="text" class="form-control" placeholder="请在这里输入你的新的邮箱" name="newmail">
  <span class="input-group-addon" id="basic-addon1">获取验证码</span>
</div>

    <div class="form-group input-group yan">
  <input type="text" class="form-control" placeholder="验证码" name="yan_zheng">
</div>
    <button id="submit_email" class="btn btn-primary">确认修改</button>
</form>

{% endblock%}